Skip to content

fix: support single-level and default packages for all runnables#254

Merged
tartarughina merged 5 commits into
zed-extensions:mainfrom
fredyw:single_package_bugfix
May 18, 2026
Merged

fix: support single-level and default packages for all runnables#254
tartarughina merged 5 commits into
zed-extensions:mainfrom
fredyw:single_package_bugfix

Conversation

@fredyw
Copy link
Copy Markdown
Contributor

@fredyw fredyw commented May 18, 2026

Fix the issue where Zed fails to recognize "Run" and "Test" buttons for Java files in single-level packages, e.g., package example; or the default package (no package declared).

Changes:

  • runnables.scm: Update the package query to match both simple identifier and scoped_identifier nodes. Make the package declaration optional across all patterns (main methods, JUnit test methods, and test classes) to enable default package support.
  • tasks.json: Add conditional shell logic (if [ -n "$package" ]...) to correctly construct the fully qualified class name. This prevents the generation of invalid commands (like java .Main) when executing code in the default package.

Testing:

  • Replace manual assertions with insta snapshot tests in tests/java.rs for better maintainability and alignment with core Zed extensions.
  • Introduce a complete matrix of Java fixtures (multi-level, single-level, and default package) to verify both Tree-sitter captures and task command generation.

Fixes #241.

The tree-sitter query for runnables was only capturing `scoped_identifier` for package names.
This caused it to completely miss single-level packages, e.g., `package example;`, which are
parsed as a basic `identifier`.

This updates the query to match both `identifier` and `scoped_identifier`.
@cla-bot
Copy link
Copy Markdown

cla-bot Bot commented May 18, 2026

We require contributors to sign our Contributor License Agreement, and we don't have @fredyw on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@fredyw
Copy link
Copy Markdown
Contributor Author

fredyw commented May 18, 2026

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed label May 18, 2026
@cla-bot
Copy link
Copy Markdown

cla-bot Bot commented May 18, 2026

The cla-bot has been summoned, and re-checked this pull request!

@tartarughina
Copy link
Copy Markdown
Collaborator

@fredyw thanks for the contribution.

Could you expand the description explaining all the additions included in the PR?

@tartarughina
Copy link
Copy Markdown
Collaborator

Consider also to extend the PR covering what #248 does for files without a package

@fredyw fredyw changed the title fix: Support single-level packages in runnables fix: support single-level and default packages for all runnables May 18, 2026
@fredyw
Copy link
Copy Markdown
Contributor Author

fredyw commented May 18, 2026

@fredyw thanks for the contribution.

Could you expand the description explaining all the additions included in the PR?

Done.

@fredyw
Copy link
Copy Markdown
Contributor Author

fredyw commented May 18, 2026

Consider also to extend the PR covering what #248 does for files without a package

Done. I have incorporated your changes in this PR. I also added missing tests.

@tartarughina
Copy link
Copy Markdown
Collaborator

Thanks for the additions, both in the code and in the PR description.

Let's include nested tests for full feature coverage and then we can proceed merging the changes.

PS. Run `just lint' a couple of times to be sure the checks for the PR become green.

@tartarughina
Copy link
Copy Markdown
Collaborator

Also double check that everything works as expected by double testing against, there might be something off as all main executes

> Task :run
Dotted package main: com.example

even though I executed the main from the file SimpleMain

runnable-test.tar.gz

@fredyw
Copy link
Copy Markdown
Contributor Author

fredyw commented May 18, 2026

Also double check that everything works as expected by double testing against, there might be something off as all main executes

> Task :run
Dotted package main: com.example

even though I executed the main from the file SimpleMain

runnable-test.tar.gz

That's because you have build.gradle that looks like this.

application {
    mainClass = 'com.example.DottedMain'
}

The existing tasks.json expects the -PmainClass to be set. To be honest, this isn't a standard in Gradle and unlike Maven where we can specify -Dexec.mainClass, in Gradle, we have to configure the application to look like this to be able to pass -PmainClass.

application {
    mainClass = project.findProperty("mainClass") ?: "com.example.DefaultMain"
}

One we have that configured, it will work just correctly when running SimpleMain.

> Task :run
Simple package main: org

@fredyw
Copy link
Copy Markdown
Contributor Author

fredyw commented May 18, 2026

Thanks for the additions, both in the code and in the PR description.

Let's include nested tests for full feature coverage and then we can proceed merging the changes.

PS. Run `just lint' a couple of times to be sure the checks for the PR become green.

Nested tests added. I also fixed the format and linter errors.

@tartarughina
Copy link
Copy Markdown
Collaborator

@fredyw oh thanks for pointing that out, I completely missed that from the gradle file. Kudos for spotting that.

@tartarughina tartarughina merged commit d0d3a1f into zed-extensions:main May 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test runnables don't match simple (non-dotted) package names

2 participants